﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Microsoft.Win32;

namespace CryForLua_wpf_AvEd.UserPreferences
{
    public class UserPreferencesData
    {
        private static UserPreferencesData _instance;

        private UserPreferencesData() { }

        public static UserPreferencesData Instance
        {
            get
            {
                lock (typeof(UserPreferencesData))
                {
                    if (_instance == null)
                        _instance = new UserPreferencesData();

                    return _instance;
                }
            }
        }

        public void Init(string rootDir, Color fCol, Color lCol, Color cCol)
        {
            cryEngineRootDir = rootDir;
            functionColor = fCol;
            loopColor = lCol;
            conditionColor = cCol;
        }

        public void Reset()
        {
            string rootdir;
            RegistryKey crySettings = Registry.CurrentUser.OpenSubKey("Software\\Crytek\\Settings");

            if (crySettings != null)
            {
                rootdir = (string)crySettings.GetValue("ENG_RootPath");
            }
            else
                rootdir = "C";

            cryEngineRootDir = rootdir;
            functionColor = Color.Blue;
            loopColor = Color.Blue;
            conditionColor = Color.Blue;
            commentColor = Color.Green;
            textColor = Color.Black;
            useAddDir = false;
            for (int i = 0; i < 7; ++i)
            {
                addDir[i] = "<None>";
            }

            conditionFont = new Font("Courier New", 12.0f, FontStyle.Regular, GraphicsUnit.Point);//(FontFamily.GenericSansSerif, 12.0f, FontStyle.Regular);
            loopFont = new Font("Courier New", 12.0f, FontStyle.Regular, GraphicsUnit.Point);
            functionFont = new Font("Courier New", 12.0f, FontStyle.Regular, GraphicsUnit.Point);
            commentFont = new Font("Courier New", 12.0f, FontStyle.Regular, GraphicsUnit.Point);
            textFont = new Font("Courier New", 12.0f, FontStyle.Regular, GraphicsUnit.Point);
            htmFilesDir = "";


        }

        public bool ReadUserSettingsFromFile()
        {
            // Reset needed for CryEngine Root Path
            Reset();
            // Check if file exists..

            string appDir = Application.UserAppDataPath;

            string[] filePaths = Directory.GetFiles(appDir, "preferences.xml", SearchOption.AllDirectories);

            // If not create the default one
            if (filePaths.Length == 0)
            {
                SafePreferencesToFile(); // This works because defaults are always initialised before the file is even loaded
            }

            FontConverter cv = new FontConverter();
            XmlTextReader preferencesReader = new XmlTextReader(appDir + "\\preferences.xml");

            if (preferencesReader == null)
            {
            }

            preferencesReader.Read();

            while (preferencesReader.Read())
            {
                preferencesReader.MoveToElement();

                switch (preferencesReader.NodeType)
                {
                    case (XmlNodeType.Element):
                        {

                            switch (preferencesReader.Name)
                            {

                                case ("htmFilesDir"):
                                    {
                                        htmFilesDir = preferencesReader.ReadElementContentAsString();
                                    }break;

                                case ("cryEngRootDir"):
                                    {
                                       // cryEngineRootDir = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("useAddDir"):
                                    {
                                        useAddDir = preferencesReader.ReadElementContentAsBoolean();
                                    } break;

                                #region Additional Directories
                                case ("addDir0"):
                                    {
                                        addDir[0] = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("addDir1"):
                                    {
                                        addDir[1] = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("addDir2"):
                                    {
                                        addDir[2] = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("addDir3"):
                                    {
                                        addDir[3] = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("addDir4"):
                                    {
                                        addDir[4] = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("addDir5"):
                                    {
                                        addDir[5] = preferencesReader.ReadElementContentAsString();
                                    } break;
                                case ("addDir6"):
                                    {
                                        addDir[6] = preferencesReader.ReadElementContentAsString();
                                    } break;

                                #endregion
                            }
                        } break;
                }
            }

            preferencesReader.Close();
            return true;
        }
   
        public void SafePreferencesToFile()
        {
           
            // Write down the data
            FontConverter cv = new FontConverter();

            XmlTextWriter writer = new XmlTextWriter(Application.UserAppDataPath.ToString() + "\\preferences.xml", System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument(false);



            writer.WriteComment("Cry for LUA preferences");
            writer.WriteComment("NOTE: Color values are written in ToArgb()");

            writer.WriteStartElement("root");

            //writer.WriteElementString("functioncolor", Pref_Data.functionColor.ToArgb().ToString());
            //writer.WriteElementString("functionfont", cv.ConvertToString(Pref_Data.functionFont));

            //writer.WriteElementString("loopcolor", Pref_Data.loopColor.ToArgb().ToString());
            //writer.WriteElementString("loopfont", cv.ConvertToString(Pref_Data.loopFont));

            //writer.WriteElementString("conditioncolor", Pref_Data.conditionColor.ToArgb().ToString());
            //writer.WriteElementString("conditionfont", cv.ConvertToString(Pref_Data.conditionFont));

            //writer.WriteElementString("commentcolor", Pref_Data.commentColor.ToArgb().ToString());
            //writer.WriteElementString("commentfont", cv.ConvertToString(Pref_Data.commentFont));

            //writer.WriteElementString("textcolor", Pref_Data.textColor.ToArgb().ToString());
            //writer.WriteElementString("textfont", cv.ConvertToString(Pref_Data.textFont));


            writer.WriteElementString("cryEngRootDir", this.cryEngineRootDir);
            writer.WriteElementString("useAddDir", this.useAddDir ? "1" : "0");
            writer.WriteElementString("htmFilesDir", this.htmFilesDir);


            // Counting from 0 of course
            for (int i = 0; i < 7; ++i)
            {
                writer.WriteElementString("addDir" + i.ToString(), addDir[i]);
            }

            #region new element (left for reference)
            //writer.WriteStartElement("TESDT");

            //writer.WriteStartAttribute("edSDGFS");

            //writer.WriteValue("Hello");

            //writer.WriteEndAttribute();


            //writer.WriteStartAttribute("sdfsdf");

            //writer.WriteValue("Hdsfsdfello");

            //writer.WriteEndAttribute();

            //writer.WriteEndElement();
            #endregion


            writer.Flush();
            writer.Close();
        }


        public string cryEngineRootDir;
        public Color functionColor;
        public Color loopColor;
        public Color conditionColor;
        public Color commentColor;
        public Color textColor;
        public bool useAddDir;
        public string[] addDir = new string[7];
        public Font conditionFont;
        public Font loopFont;
        public Font functionFont;
        public Font commentFont;
        public Font textFont;
        public string htmFilesDir;
    }
}